import requests
from bs4 import BeautifulSoup

# 定义请求头，模拟浏览器访问
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}

# 定义获取章节链接的函数
def get_chapter_list(catalog_url):
    response = requests.get(catalog_url, headers=headers)
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'html.parser')
    # 使用更准确的选择器定位章节链接
    chapter_links = soup.select('dd a')
    start_index = -1
    end_index = -1
    for i, link in enumerate(chapter_links):
        if '第1章 戏鬼回家' in link.text:
            start_index = i
        if '第1373章 进击的贞子' in link.text:
            end_index = i
    if start_index == -1 or end_index == -1:
        print('未找到指定的起始或结束章节，请检查页面结构。')
        return []
    selected_links = chapter_links[start_index:end_index + 1]
    chapter_urls = []
    for link in selected_links:
        chapter_url = requests.compat.urljoin(catalog_url, link['href'])
        chapter_urls.append(chapter_url)
    return chapter_urls

# 定义获取章节内容的函数
def get_chapter_content(chapter_url):
    response = requests.get(chapter_url, headers=headers)
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'html.parser')
    # 根据实际页面结构获取标题和内容
    title = soup.find('h1').text if soup.find('h1') else '无标题'
    content_div = soup.find('div', id='content')
    content = content_div.get_text(separator='\n').strip() if content_div else ''
    return title, content

# 主函数
def main():
    catalog_url = 'https://www.xs7.com/shu/127762/'
    chapter_urls = get_chapter_list(catalog_url)
    if chapter_urls:
        with open('novel.txt', 'w', encoding='utf-8') as f:
            for url in chapter_urls:
                title, content = get_chapter_content(url)
                f.write(f'{"="*20}\n{title}\n{"="*20}\n\n')
                f.write(content + '\n\n')
                print(f'已下载章节: {title}')
        print('小说下载完成，已保存到 novel.txt 文件中。')

if __name__ == '__main__':
    main()
